package MLTool;

import manager.ConnectionManager;
import android.content.Intent;
import Content.ContentConfig;
import Interceptor.RequestInterceptor;
import Interceptor.ResponseInterceptor;
import SyncPool.CallBack;
import SyncPool.HttpLifeCycleInterface;
import SyncPool.ServicePool;
import SyncPool.URLConfig;

public class MHttpClient {
	RequestInterceptor requestInterceptor;
	ResponseInterceptor responseInterceptor;

	public MHttpClient addRequestInterceptor(RequestInterceptor requestInterceptor) {
		if (this.requestInterceptor == null)
			this.requestInterceptor = requestInterceptor;
		else
			requestInterceptor.setRequestInterceptor(this.requestInterceptor);

		this.requestInterceptor = requestInterceptor;

		return this;
	}

	public MHttpClient addResponseInterceptor(
			ResponseInterceptor responseInterceptor) {
		if (this.responseInterceptor == null)
			this.responseInterceptor = responseInterceptor;
		else
			responseInterceptor
					.setResponseInterceptor(this.responseInterceptor);
		this.responseInterceptor=responseInterceptor;

		return this;
	}

	public ConnectionManager newRequest(URLConfig config,
			HttpLifeCycleInterface httpLifeCycleInterface) {

		CallBack callBack = new CallBack(config, httpLifeCycleInterface);

		ConnectionManager requstManager = callBack.getRequstManager();

		requstManager.addRequestInterceptor(this.requestInterceptor);
		
		requstManager.addResponseInterceptor(responseInterceptor);
		
		

		Intent intent = new Intent(MLApplication.newInstance(),
				ServicePool.class);
		intent.putExtra(ContentConfig.STARTSERVICE, callBack);
		MLApplication.newInstance().startService(intent);
		return requstManager;
	}

}
